﻿using System.Data;
using System.IO;
using System.Threading;

namespace AshuSoft.Test
{
    public class TableDataViewer
    {
        /// <summary>
        /// Shows the table data in an new window on a new thread
        /// </summary>
        /// <param name="table">The table whose data is to be shown</param>
        /// <param name="captionString">The caption to identify this table/window</param>
        public static void ShowTable(DataTable table, string captionString)
        {
            object[] obj = new object[2];
            obj[0] = table;
            obj[1] = captionString;
            var thread = new Thread(ShowDataTableWindowOnThread);
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start(obj);
        }

        /// <summary>
        /// Shows the complete data set in an new window in an new thread
        /// </summary>
        /// <param name="dataSet">The data set to show</param>
        public static void ShowDataSet(DataSet dataSet)
        {
            object[] obj = new object[2];
            obj[0] = dataSet;
            obj[1] = "";
            var thread = new Thread(ShowDataSetWindowOnThread);
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start(obj);
        }

        /// <summary>
        /// Shows the complete data set in an new window in an new thread
        /// </summary>
        /// <param name="dataSet">The data set to show</param>
        /// <param name="customString">Custom data related to the data set to be shown in window.</param>
        public static void ShowDataSet(DataSet dataSet, string customString)
        {
            object[] obj = new object[2];
            obj[0] = dataSet;
            obj[1] = customString;
            var thread = new Thread(ShowDataSetWindowOnThread);
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start(obj);
        }

        /// <summary>
        /// Shows the complete data set in an new window in an new thread
        /// </summary>
        /// <param name="dataSet">The data set to show</param>
        /// <param name="dataSetFileName">The path for this dataset where user can save changes.</param>
        /// <param name="customString">Custom data related to the data set to be shown in window.</param>
        public static void ShowDataSet(DataSet dataSet, string dataSetFileName, string customString)
        {
            object[] obj = new object[3];
            obj[0] = dataSet;
            obj[1] = dataSetFileName;
            obj[2] = customString;
            var thread = new Thread(ShowDataSetWindowOnThread);
            thread.Start(obj);
        }

        /// <summary>
        /// Shows the complete data set in an new window in an new thread
        /// </summary>
        /// <param name="dataSetFileName">The file containing the exported data set</param>
        /// <returns>Returns the data set loaded from the passed file name</returns>
        public static DataSet ShowDataSet(string dataSetFileName)
        {
            DataSet dataSet = new DataSet(Path.GetFileNameWithoutExtension(dataSetFileName));
            dataSet.ReadXml(dataSetFileName);
            object[] obj = new object[3];
            obj[0] = dataSet;
            obj[1] = dataSetFileName;
            obj[2] = "";
            var thread = new Thread(ShowDataSetWindowOnThread);
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start(obj);
            return dataSet;
        }

        /// <summary>
        /// Shows the complete data set in an new window in an new thread
        /// </summary>
        /// <param name="dataSetFileName">The file containing the exported data set</param>
        /// <param name="customString">Custom data related to the data set to be shown in window.</param>
        /// <returns>Returns the data set loaded from the passed file name</returns>
        public static DataSet ShowDataSet(string dataSetFileName, string customString)
        {
            DataSet dataSet = new DataSet(Path.GetFileNameWithoutExtension(dataSetFileName));
            dataSet.ReadXml(dataSetFileName);
            object[] obj = new object[3];
            obj[0] = dataSet;
            obj[1] = dataSetFileName;
            obj[2] = customString;
            var thread = new Thread(ShowDataSetWindowOnThread);
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start(obj);
            return dataSet;
        }

        
        static void ShowDataSetWindowOnThread(object param)
        {
            object[] obj = (object[]) param;
            if (obj.Length == 2)
            {
                (new DataSetViewerForm((DataSet) obj[0], obj[1].ToString())).ShowDialog();
            }
            else if (obj.Length == 3)
            {
                (new DataSetViewerForm((DataSet)obj[0], (string)obj[1], obj[2].ToString())).ShowDialog();
            }
        }

        static void ShowDataTableWindowOnThread(object obj)
        {
            DataTable table = (DataTable)(((object[])obj)[0]);
            string captionString = (string)(((object[])obj)[1]);
            (new TableDataViewerForm(table, captionString)).ShowDialog();
        }
    }
}
